package home;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;

import java.util.ArrayList;
import java.util.List;

import adapter.MainAdapter1;
import base.BasePage;
import bean.MainFunBean;
import httptool.CommonUtil;
import httptool.QLParser;
import httptool.SharePrefUtil;
import httptool.takeUrlResource;
import pullrefreshview.PullToRefreshBase;
import pullrefreshview.PullToRefreshListView;
import shanshuitiancheng.news.R;
import shanshuitiancheng.news.WebViewActivity;

/**
 * Created by cch on 2016/6/22.
 */

public class FunctionPage1 extends BasePage {
    private  PullToRefreshListView ptrlv;
    private  MainAdapter1 adapter;
    private  String dataUrls;
    private String much;

    public FunctionPage1(Context ct,String dataUrls) {
        super(ct);
        this.dataUrls = dataUrls;
    }

    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.main_layout,null);
        ptrlv = (PullToRefreshListView) view.findViewById(R.id.lv);
        ptrlv.setPullLoadEnabled(false);
        ptrlv.setScrollLoadEnabled(true);
        setLastUpdateTimes();

        ptrlv.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MainFunBean.NewsBean newsBean;
                newsBean = newsdatas.get(position);
                Intent intent = new Intent(ct, WebViewActivity.class);
                intent.putExtra("url",newsBean.getUrl());
                ct.startActivity(intent);
            }
        });

        //下拉刷新跟上拉加载的点击事件
        ptrlv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                getJson(dataUrls, true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getJson(much, false);
            }
        });
        return view;
    }

    //当触动点击事件时候调用getjson（）方法，没有点击没有触发
    private List<MainFunBean.NewsBean> newsdatas = new ArrayList<>();
    private void getJson(String urls, final boolean b) {
        takeUrlResource.getJson(ct, urls, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (!s.contains("Warning")) {
                    SharePrefUtil.saveString(ct, dataUrls, s);
                    MainFunBean datas = QLParser.parse(s, MainFunBean.class);
                    if (datas.getRetcode() == 200) {
                        List<MainFunBean.NewsBean> news = datas.getNews();
                        if (b) {
                            newsdatas.clear();
                            newsdatas.addAll(news);
                        }else {
                            newsdatas.addAll(news);
                        }

                        much = datas.getMore();
                        if (adapter == null){
                            adapter = new MainAdapter1(newsdatas,ct);
                            ptrlv.getRefreshableView().setAdapter(adapter);
                        }else{
                            adapter.notifyDataSetChanged();
                        }
                        onLoader();
                        ptrlv.setHasMoreData(true);
                        setLastUpdateTimes();
                    }
                }
            }
        });
    }

    @Override
    public void initData() {
        String data = SharePrefUtil.getString(ct,dataUrls,"");

        if (TextUtils.isEmpty(data)){
            getJson(dataUrls,true);
        }else {
            if (!data.contains("Waring")){
                MainFunBean datas = QLParser.parse(data,MainFunBean.class);
                if (datas.getRetcode() == 200){
                    List<MainFunBean.NewsBean> news = datas.getNews();
                    much = datas.getMore();
                    if (adapter == null){
                        adapter = new MainAdapter1(news,ct);
                        ptrlv.getRefreshableView().setAdapter(adapter);
                    }
                }
            }
        }
    }

    private  void onLoader(){
        ptrlv.onPullUpRefreshComplete();
        ptrlv.onPullDownRefreshComplete();
    }

    private void setLastUpdateTimes(){
       String text = CommonUtil.getStringDate();
        ptrlv.setLastUpdatedLabel(text);
    }
}
//    private  Context ct;
//    private  String  dataUrls;
//    private View view;
//    private PullToRefreshListView ptrlv;
//    private MainAdapter1 adapter;
//
//
//    public FunctionPage1(Context ct,String dataUrls) {
//        super(ct);
//        this.dataUrls = dataUrls;
//    }
//
//    @Override
//    public View initView(LayoutInflater inflater) {
//        view = inflater.inflate(R.layout.main_layout, null);
//        ptrlv = (PullToRefreshListView) view.findViewById(R.id.lv);
//        ptrlv.setPullLoadEnabled(false);
//        ptrlv.setScrollLoadEnabled(true);
//        setLastUpdataTime();
//        ptrlv.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                MainFunBean.NewsBean newsBean;
//
//            }
//        });
//
//        ptrlv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
//            @Override
//            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//                getJson(dataUrls,true);
//            }
//
//            @Override
//            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//                getJson(much,false);
//            }
//        });
//        return view;
//    }
//
//    private String much;
//    private List<MainFunBean.NewsBean> newsData = new ArrayList<>();
//    //联网请求网络
//    private void getJson(final String dataUrls, final boolean b) {
//        takeUrlResource.getJson(ct, dataUrls, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String s) {
//                if (!s.contains("Warning")){
//                    SharePrefUtil.saveString(ct,dataUrls,s);
//                    MainFunBean datas = QLParser.parse(s,MainFunBean.class);
//                    if (datas.getRetcode() == 200){
//                        List<MainFunBean.NewsBean> news = datas.getNews();
//                        //判断是否是第一次请求当前url数据
//                        if (b){
//                            newsData.clear();
//                            newsData.addAll(news);
//                        }else{
//                            newsData.addAll(news);
//                        }
//
//                        much = datas.getMore();
//                        if (adapter == null){
//                            adapter = new MainAdapter1(newsData,ct);
//                            ptrlv.getRefreshableView().setAdapter(adapter);
//                        }else {
//                            adapter.notifyDataSetChanged();
//                        }
//                        onLoaded();
//                        ptrlv.setHasMoreData(true);
//                        setLastUpdataTime();
//                    }
//                }
//            }
//        });
//    }
//
//    @Override
//    public void initData() {
//        String data = SharePrefUtil.getString(ct,dataUrls,"");
//        if (TextUtils.isEmpty(data)){
//            getJson(dataUrls,true);
//        }else{
//            if (!data.contains("Warning")){
//                MainFunBean datas = QLParser.parse(data,MainFunBean.class);
//                if (datas.getRetcode() == 200){
//                    List<MainFunBean.NewsBean> news = datas.getNews();
//
//                    much = datas.getMore();
//                    if (adapter == null){
//                        adapter = new MainAdapter1(news,ct);
//                        ptrlv.getRefreshableView().setAdapter(adapter);
//                    }
//                }
//            }
//        }
//    }
//
//    private  void onLoaded(){
//        ptrlv.onPullUpRefreshComplete();
//        ptrlv.onPullDownRefreshComplete();
//    }
//
//    private  void setLastUpdataTime(){
//        String text = CommonUtil.getStringDate();
//        ptrlv.setLastUpdatedLabel(text);
//    }
//}
